@@ -1,10 +1,11 @@ |
||
1 | 1 |
module Agents |
2 | 2 |
class PostAgent < Agent |
3 |
- cannot_be_scheduled! |
|
4 | 3 |
cannot_create_events! |
5 | 4 |
|
5 |
+ default_schedule "never" |
|
6 |
+ |
|
6 | 7 |
description <<-MD |
7 |
- Post Agent receives events from other agents and send those events as the contents of a post request to a specified url. `post_url` field must specify where you would like to receive post requests and do not forget to include URI scheme (`http` or `https`) |
|
8 |
+ A PostAgent receives events from other agents (or runs periodically), merges those events with the contents of `payload`, and sends the results as POST requests to a specified url. The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`). |
|
8 | 9 |
MD |
9 | 10 |
|
10 | 11 |
event_description "Does not produce events." |
@@ -12,7 +13,10 @@ module Agents |
||
12 | 13 |
def default_options |
13 | 14 |
{ |
14 | 15 |
'post_url' => "http://www.example.com", |
15 |
- 'expected_receive_period_in_days' => 1 |
|
16 |
+ 'expected_receive_period_in_days' => 1, |
|
17 |
+ 'payload' => { |
|
18 |
+ 'key' => 'value' |
|
19 |
+ } |
|
16 | 20 |
} |
17 | 21 |
end |
18 | 22 |
|
@@ -24,19 +28,29 @@ module Agents |
||
24 | 28 |
unless options['post_url'].present? && options['expected_receive_period_in_days'].present? |
25 | 29 |
errors.add(:base, "post_url and expected_receive_period_in_days are required fields") |
26 | 30 |
end |
27 |
- end |
|
28 | 31 |
|
29 |
- def post_event(uri, event) |
|
30 |
- req = Net::HTTP::Post.new(uri.request_uri) |
|
31 |
- req.form_data = event |
|
32 |
- Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) } |
|
32 |
+ if options['payload'].present? && !options['payload'].is_a?(Hash) |
|
33 |
+ errors.add(:base, "if provided, payload must be a hash") |
|
34 |
+ end |
|
33 | 35 |
end |
34 | 36 |
|
35 | 37 |
def receive(incoming_events) |
36 | 38 |
incoming_events.each do |event| |
37 |
- uri = URI options[:post_url] |
|
38 |
- post_event uri, event.payload |
|
39 |
+ post_data (options['payload'].presence || {}).merge(event.payload) |
|
39 | 40 |
end |
40 | 41 |
end |
42 |
+ |
|
43 |
+ def check |
|
44 |
+ post_data options['payload'].presence || {} |
|
45 |
+ end |
|
46 |
+ |
|
47 |
+ private |
|
48 |
+ |
|
49 |
+ def post_data(data) |
|
50 |
+ uri = URI.new(options[:post_url]) |
|
51 |
+ req = Net::HTTP::Post.new(uri.request_uri) |
|
52 |
+ req.form_data = data |
|
53 |
+ Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) } |
|
54 |
+ end |
|
41 | 55 |
end |
42 | 56 |
end |
@@ -5,8 +5,11 @@ describe Agents::PostAgent do |
||
5 | 5 |
@valid_params = { |
6 | 6 |
:name => "somename", |
7 | 7 |
:options => { |
8 |
- :post_url => "http://www.example.com", |
|
9 |
- :expected_receive_period_in_days => 1 |
|
8 |
+ 'post_url' => "http://www.example.com", |
|
9 |
+ 'expected_receive_period_in_days' => 1, |
|
10 |
+ 'payload' => { |
|
11 |
+ 'default' => 'value' |
|
12 |
+ } |
|
10 | 13 |
} |
11 | 14 |
} |
12 | 15 |
|
@@ -17,28 +20,42 @@ describe Agents::PostAgent do |
||
17 | 20 |
@event = Event.new |
18 | 21 |
@event.agent = agents(:jane_weather_agent) |
19 | 22 |
@event.payload = { |
20 |
- :somekey => "somevalue", |
|
21 |
- :someotherkey => { |
|
22 |
- :somekey => "value" |
|
23 |
+ 'somekey' => 'somevalue', |
|
24 |
+ 'someotherkey' => { |
|
25 |
+ 'somekey' => 'value' |
|
23 | 26 |
} |
24 | 27 |
} |
25 | 28 |
|
26 | 29 |
@sent_messages = [] |
27 |
- stub.any_instance_of(Agents::PostAgent).post_event { |uri, event| @sent_messages << event } |
|
30 |
+ stub.any_instance_of(Agents::PostAgent).post_data { |event| @sent_messages << event } |
|
28 | 31 |
end |
29 | 32 |
|
30 | 33 |
describe "#receive" do |
31 |
- it "checks if it can handle multiple events" do |
|
34 |
+ it "can handle multiple events and merge the payloads with options['payload']" do |
|
32 | 35 |
event1 = Event.new |
33 | 36 |
event1.agent = agents(:bob_weather_agent) |
34 | 37 |
event1.payload = { |
35 |
- :xyz => "value1", |
|
36 |
- :message => "value2" |
|
38 |
+ 'xyz' => 'value1', |
|
39 |
+ 'message' => 'value2', |
|
40 |
+ 'default' => 'value2' |
|
37 | 41 |
} |
38 | 42 |
|
39 | 43 |
lambda { |
40 | 44 |
@checker.receive([@event, event1]) |
41 | 45 |
}.should change { @sent_messages.length }.by(2) |
46 |
+ |
|
47 |
+ @sent_messages[0].should == @event.payload.merge('default' => 'value') |
|
48 |
+ @sent_messages[1].should == event1.payload |
|
49 |
+ end |
|
50 |
+ end |
|
51 |
+ |
|
52 |
+ describe "#check" do |
|
53 |
+ it "sends options['payload']" do |
|
54 |
+ lambda { |
|
55 |
+ @checker.check |
|
56 |
+ }.should change { @sent_messages.length }.by(1) |
|
57 |
+ |
|
58 |
+ @sent_messages[0].should == @checker.options['payload'] |
|
42 | 59 |
end |
43 | 60 |
end |
44 | 61 |
|
@@ -67,5 +84,19 @@ describe Agents::PostAgent do |
||
67 | 84 |
@checker.options[:expected_receive_period_in_days] = "" |
68 | 85 |
@checker.should_not be_valid |
69 | 86 |
end |
87 |
+ |
|
88 |
+ it "should validate payload as a hash, if present" do |
|
89 |
+ @checker.options[:payload] = "" |
|
90 |
+ @checker.should be_valid |
|
91 |
+ |
|
92 |
+ @checker.options[:payload] = "hello" |
|
93 |
+ @checker.should_not be_valid |
|
94 |
+ |
|
95 |
+ @checker.options[:payload] = ["foo", "bar"] |
|
96 |
+ @checker.should_not be_valid |
|
97 |
+ |
|
98 |
+ @checker.options[:payload] = { 'this' => 'that' } |
|
99 |
+ @checker.should be_valid |
|
100 |
+ end |
|
70 | 101 |
end |
71 | 102 |
end |